package com.laikan.reader.bookfriend.base;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import com.laikan.reader.bookfriend.holder.LViewHolder;

import java.util.List;

/**
 * Created by admin on 2016/10/31.
 * 通用的数据适配器recycleView
 */
public abstract class CommonAdapter<T> extends RecyclerView.Adapter<LViewHolder>{

    protected Context mContext;
    protected int mLayoutId;
    protected List<T> mDatas;

    public CommonAdapter(Context context, int layoutId, List<T> datas){
        mContext = context;
        mLayoutId = layoutId;
        setData(datas);
    }


    public void addData(List list) {
        if (null != mDatas && mDatas.size()>0){
            mDatas.addAll(list);
        }else{
            setData(list);
        }
    }

    public void setData(List<T> mDatas) {
        this.mDatas = mDatas;
    }

    public List getData() {
        return mDatas;
    }

    public void clear() {
        if (null != mDatas){
            mDatas.clear();
        }
    }

    @Override
    public LViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final LViewHolder mHolder = LViewHolder.createViewHolder(mContext,mLayoutId);
        return mHolder;
    }

    @Override
    public void onBindViewHolder(LViewHolder holder, int position) {
        convert(holder,mDatas.get(position));
    }

    protected abstract void convert(LViewHolder holder, T t);

    @Override
    public int getItemCount() {
        return null == mDatas ? 0 : mDatas.size();
    }

}
